package logic

import (
	"context"
	"fmt"
	"person/mobels/mysql"
	"person/pkg"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *personrpc.UserPerfectRequest) (*personrpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Where("account = ?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在，禁止修改")
	}

	if user.Password != pkg.Md5(in.Password) {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}

	user = mysql.User{
		Sex:  in.Sex,
		Name: in.Name,
	}
	mysql.DB.Updates(&user)
	return &personrpc.UserPerfectResponse{Id: int64(user.ID)}, nil
}
